import time
import Page
# import requests
from Page import Launcher
from Page.Launcher.LauncherUserPage import LauncherUserPage
from Page.Launcher.LauncherPasswordPage import LauncherPasswordPage
from Base.Base import Base
from selenium.webdriver.common.by import By


class LauncherIndexPage(Base):
    """
    桌面首页页面

    """

    def __init__(self, driver):
        Base.__init__(self, driver)

        # 首页列表按钮
        self.index_app_list_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_go_app_list"))

        # 首页音乐标题
        self.index_music_name_text = (By.XPATH, Launcher.RESOURCE_ID.format("tv_music_name"))

        # 首页音乐歌手
        self.index_singer_name_text = (By.XPATH, Launcher.RESOURCE_ID.format("tv_music_singer"))

        # 首页用户头像
        self.index_person_icon_img = (By.XPATH, Launcher.RESOURCE_ID.format("tv_music_singer"))

        # 首页用户名称
        self.index_person_name_text = (By.XPATH, Launcher.RESOURCE_ID.format("tv_name"))

        # 桌面音乐封面(暂不封装)
        self.index_music_cover_img = (By.XPATH, Launcher.RESOURCE_ID.format("viewpager"))

        # 桌面歌词视图
        self.index_lyric_view = (By.XPATH, Launcher.RESOURCE_ID.format("lrc_view"))

        # 桌面地图
        self.index_map_img = (By.XPATH, Launcher.RESOURCE_ID.format("iv_map_init"))

        # 桌面服务
        self.index_service_img = (By.XPATH, Launcher.RESOURCE_ID.format("iv_service_init"))

        # 桌面当前播放时间
        self.index_start_time = (By.XPATH, Launcher.RESOURCE_ID.format("tv_cur_time"))

        # 桌面收藏按钮
        self.index_collection_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_favourite"))

        # 桌面上一曲按钮
        self.index_prev_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_previous"))

        # 桌面播放按钮
        self.index_play_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_play"))

        # 桌面下一曲按钮
        self.index_next_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_next"))

        # 桌面歌词按钮
        self.index_lyric_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_lyric"))

        # 桌面听歌识曲按钮
        self.index_ting_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_lyric"))

        # 桌面地图搜索按钮
        self.index_map_search_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_search_map"))

        # 桌面地图首页按钮
        self.index_map_home_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_go_home"))

        # 桌面服务搜索按钮
        self.index_service_search_btn = (By.XPATH, Launcher.RESOURCE_ID.format("iv_search_service"))

        # 推荐弹窗标题
        self.index_recommend_title_text = (By.XPATH, Launcher.RESOURCE_ID.format("dialog_title"))

        # 推荐弹窗选项, `通勤模式`
        self.index_recommend_item_text = (By.XPATH, Launcher.RESOURCE_ID.format("tv_work"))

    def add_page_launcher_user(self):
        """
        获取用户页

        :return:
        """
        self.launcher_user_page = LauncherUserPage(self.driver)

    def add_page_launcher_password(self):
        """
        获取密码页

        :return:
        """
        self.launcher_password_page = LauncherPasswordPage(self.driver)

    def get_music_name(self):
        """
        获取当前播放节目名

        :return:
        """
        return self.find_element_o(self.index_music_name_text)

    def get_person_name(self):
        """
        获取用户名

        :return:
        """
        return self.find_element_o(self.index_person_name_text)

    def get_cover_img(self):
        """
        获取封面

        :return:
        """
        return self.find_element_o(self.index_music_cover_img)

    def get_lyric_view(self):
        """
        获取歌词视图

        :return:
        """
        return self.find_element_o(self.index_lyric_view)

    def get_index_service(self):
        """
        获取首页服务图片

        :return:
        """
        return self.find_element_o(self.index_service_img)

    def get_start_time(self):
        """
        获取当前节目的播放时长

        :return:
        """
        return self.find_element_o(self.index_start_time)

    def click_app_list(self):
        """
        点击应用列表

        :return: None
        """
        self.click_element(self.index_app_list_btn)

    def click_person_name(self):
        """
        点击用户名

        :return:
        """
        self.click_element(self.index_person_name_text)

    # 元素带有NAF属性, 无法使用
    # def get_Page_collection(self):
    #     """
    #     获取桌面的收藏按钮
    #
    #     :return:
    #     """
    #     self.find_element_o(Page.Page_index_collection_btn)

    def click_collection(self):
        """
        点击收藏按钮

        :return:
        """
        if self.is_display_app_list_btn():
            self.click_element(self.index_collection_btn)

    def click_play(self):
        """
        点击播放按钮

        :return:
        """
        self.click_element(self.index_play_btn)

    def click_lyric(self):
        """
        点击歌词按钮

        :return:
        """
        self.click_element(self.index_lyric_btn)

    def click_service(self):
        """
        点击首页服务图片

        :return:
        """
        self.click_element(self.index_service_img)

    def click_service_search(self):
        """
        点击首页服务搜索按钮

        :return:
        """
        self.click_element(self.index_service_search_btn)

    def is_display_app_list_btn(self):
        """
        是否展示应用列表按钮
        :return:
        """
        return self.is_display(self.index_app_list_btn)

    def swipe_cover(self):
        """
        滑动桌面的封面

        :return:
        """
        if self.is_display_app_list_btn():
            self.swipe_o(location=(600, 600, 200, 500), direction="up")

    def choose_recommend(self):
        """
        出现弹窗时, 默认选择通勤模式

        :return:
        """
        if self.is_display(self.index_recommend_title_text):
            self.click_element(self.index_recommend_item_text)

    def login_user(self):
        """
        登录用户

        :return:
        """
        self.choose_recommend()

        if self.get_ele_text(self.get_person_name()) == '游客':
            self.click_person_name()
            self.add_page_launcher_user()
            self.launcher_user_page.click_user(1)
            self.add_page_launcher_password()
            self.launcher_password_page.login()
            res = self.is_toast_exist(text="登录成功")
            print(res)

        self.choose_recommend()

    # def push_service(self, uid, service_type='rec'):
    #     url = "http://118.89.45.153:20000/rest/pub"
    #
    #     payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"topic\"\r\n\r\n/ai/{type}/{uid}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"qos\"\r\n\r\n1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"content\"\r\n\r\n{{\n\t\"tid\": \"260d89b0-f91e-4871-921b-40110d2fcc5e\",\n\t\"cd\": 1565593862230,\n\t\"uid\": \"1073758947469074432\",\n\t\"iccid\": \"89860918710000022349\",\n\t\"imei\": \"867012039872830\",\n\t\"cid\": \"AA1000\",\n\t\"title\": \"美食推荐\",\n\t\"content\": \"这里是弹窗内容文案\",\n\t\"icon\": \"http://www.carbuyin.net/by2/appImg/38441406-9565-4039-a616-7b9c8a6689d4.png\",\n\t\"robotImage\": \"http://www.carbuyin.net/by2/appImg/38441406-9565-4039-a616-7b9c8a6689d4.png\",\n\t\"rgb\": \"#ffc800\",\n\t\"rec\": [{{\n\t\t\"type\": \"food\",\n\t\t\"index\": 2,\n\t\t\"datas\": [{{\n\t\t\t\"id\": \"0\",\n\t\t\t\"name\": \"中国元素中餐厅\",\n\t\t\t\"cityid\": \"30\",\n\t\t\t\"cityname\": \"深圳\",\n\t\t\t\"address\": \"南山区深南大道9028号益田威斯汀酒店B1楼（近世界之窗）\",\n\t\t\t\"cate\": \"美食\",\n\t\t\t\"subcate\": \"粤港菜\",\n\t\t\t\"avgscore\": \"3.9\",\n\t\t\t\"avgprice\": \"163.0\",\n\t\t\t\"area\": \"世界之窗\",\n\t\t\t\"lat\": \"22.537645\",\n\t\t\t\"lng\": \"113.976037\",\n\t\t\t\"frontimg\": \"https://p1.meituan.net/mogu/1e524acde21952fdf1d787658bc7193d514912.jpg\",\n\t\t\t\"distance\": 0.6377694091796875,\n\t\t\t\"wifi\": \"1\",\n\t\t\t\"waimai\": \"0\",\n\t\t\t\"phone\": \"0755-86348383\",\n\t\t\t\"historycoupon\": \"0\",\n\t\t\t\"iurl\": \"https://runion.meituan.com/url?key=fb51a4df9d7ed3b4c764baeb86a1113b946&url=http%3A%2F%2Fi.meituan.com%2Fshop%2Fencryptpoiid&encryptpoiid=E9ijhYrmXQoBlLWNOYpU9Q\",\n\t\t\t\"openinfo\": \"周一至周日  09:00-15:00  17:30-22:00\"\n\t\t}}]\n\t}}]\n}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"channelId\"\r\n\r\nAA1200\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
    #     payload = payload.format(uid=uid, type=service_type)
    #     print(payload)
    #     headers = {
    #         'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    #     }
    #     payload = payload.encode('utf-8')
    #     response = requests.request("POST", url, data=payload, headers=headers)
